package info.itloser.learnkotlindemo.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import info.itloser.learnkotlindemo.R
import info.itloser.learnkotlindemo.bean.Fruit

/**
 * 说明： FruitListAdapter的RecyclerView版适配器
 * 作者： itloser.info
 * 日期： 2022/3/29 17:40
 * 备注：
 */
class FruitRecyclerAdapter(private val fruits: List<Fruit>) :
    RecyclerView.Adapter<FruitRecyclerAdapter.FruitHolder>() {

    inner class FruitHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivRes: ImageView = itemView.findViewById(R.id.iv_res)
        val tvName: TextView = itemView.findViewById(R.id.tv_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FruitHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_fruit, parent, false)
        return FruitHolder(view)
    }

    override fun onBindViewHolder(holder: FruitHolder, position: Int) {
        val fruit = fruits[position]
        holder.ivRes.setImageResource(fruit.resId)
        holder.tvName.text = fruit.name
    }

    override fun getItemCount(): Int {
        return fruits.size
    }


}